/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gtree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import no.geosoft.cc.graphics.*;

/**
 *
 * @author Gustavo
 */
public class DrawTree extends JFrame implements GInteraction {

    private GScene scene_;
    private Tree tree;

    public DrawTree(Tree tree) {
        super("Tree");
        this.tree = tree;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create the GUI
        JPanel topLevel = new JPanel();
        topLevel.setLayout(new BorderLayout());
        getContentPane().add(topLevel);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JLabel("Button 1 to select color, button 2 to unselect"));

        JPanel graphicsPanel = new JPanel();
        topLevel.add(buttonPanel, BorderLayout.NORTH);

        // Create the graphic canvas
        GWindow window = new GWindow();
        topLevel.add(window.getCanvas(), BorderLayout.CENTER);

        // Create scene with default viewport and world extent settings
        scene_ = new GScene(window, "Tree");

        /*
         * double w0[] = {0.0, 1200.0, 0.0}; double w1[] = {1200.0, 1200.0,
         * 0.0}; double w2[] = {0.0, 0.0, 0.0}; scene_.setWorldExtent(w0, w1, w2);
         */
        scene_.setWorldExtent(0, 0, tree.getDimensionX() + 100, tree.getDimensionY() + 100);

        GStyle style = new GStyle();
        style.setForegroundColor(new Color(0, 0, 0));
        style.setBackgroundColor(new Color(255, 255, 255));
        style.setFont(new Font("Dialog", Font.BOLD, 14));
        scene_.setStyle(style);



        ArrayList<GObject> objects = new ArrayList<>();
        //objects.add(new gNode(scene_,tree.root().getElement(),((Node)tree.root()).getX(),((Node)tree.root()).getY()));

        recAddNode(scene_, ((Node) tree.root()), objects);

        /*
         * GObject root = (Node)tree.root(); objects.add(new
         * gNode(objects.get(0),"3",500.0,250.0)); objects.add(new
         * gNode(objects.get(0),"4",625.0,250.0)); objects.add(new
         * gNode(objects.get(2),"5",((gNode)objects.get(2)).getX()+200,600.0));
         * objects.add(new
         * gNode(objects.get(2),"6",((gNode)objects.get(2)).getX()+200,200.0));
         * objects.add(new
         * gNode(objects.get(2),"test",((gNode)objects.get(2)).getX()+200,200.0));
         * objects.add(new
         * gNode(objects.get(2),"test",((gNode)objects.get(2)).getX()+400,200.0));
         * objects.add(new
         * gNode(objects.get(2),"test",((gNode)objects.get(2)).getX()+600,200.0));
         */



        pack();
        setSize(new Dimension(500, 500));
        setVisible(true);

        window.startInteraction(this);

    }

    private ArrayList<GObject> recAddNode(GObject parent, Node n, ArrayList<GObject> array) {
        array.add(new gNode(parent, n.getElement(), n.getX(), n.getY()));
        System.out.println("ADICIONADO " + n.getElement() + " EM X-Y " + n.getX() + "-" + n.getY());
        if (n.getChildren().size() > 0) {
            GObject parent_ = array.get(array.size() - 1);
            for (int i = 0; i < n.getChildren().size(); i++) {
                recAddNode(parent_, n.getChildren().get(i), array);
            }
        }
        return array;
    }

    public void event(GScene scene, int event, int x, int y) {
        if (event == GWindow.BUTTON1_UP
                || event == GWindow.BUTTON2_UP) {
            boolean isSelected = event == GWindow.BUTTON1_UP;

            GSegment selectedSegment = scene_.findSegment(x, y);
            if (selectedSegment == null) {
                return;
            }

            GStyle style = selectedSegment.getOwner().getStyle();
            if (style == null) {
                return;
            }

            if (isSelected) {
                style.setBackgroundColor(new Color((float) Math.random(),
                        (float) Math.random(),
                        (float) Math.random()));
            } else {
                style.unsetBackgroundColor();
            }

            scene_.refresh();
        }
    }
}